import { cityApi } from "@/utils/api";

// 城市模块
const cityModule = {
  namespaced: true,
  state: {
    cityname: localStorage.getItem("name") || "北京",
    cityId: localStorage.getItem("id") || 110100,
    // 城市列表
    cityList: [],
  },
  getters: {
    hotCity(state) {
      return state.cityList[0];
    },
    otherCity(state) {
      return state.cityList.slice(1);
    },
  },
  mutations: {
    changeCity(state, payload) {
      localStorage.setItem("name", payload.name);
      localStorage.setItem("id", payload.cityId);
      state.cityname = payload.name;
      state.cityId = payload.cityId;
    },
    // 城市列表的改变
    getCityList(state, payload) {
      state.cityList = payload.result;
    },
  },
  actions: {
    // 城市列表的请求
    async getCityListAsync({ commit }) {
      const res = await cityApi();
      commit("getCityList", res);
    },
  },
};

export default cityModule;
